package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impI.CourseContentDaoImpI;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impI.CourseContentServiceImpI;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @Author wsl
 * @Date 2021/10/11 19:52
 * @Project_Name maven
 */
@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    //1.根据课程ID查询章节与课时信息
    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response){

        try {
            //1.获取参数
            String course_id = request.getParameter("course_id");

            //2.业务处理
            CourseContentDao contentDao = new CourseContentDaoImpI();
            List<Course_Section> list = contentDao.findSectionAndLessonByCourseId(Integer.parseInt(course_id));

            //3.返回结果
            String jsonResult = JSON.toJSONString(list);
            response.getWriter().print(jsonResult);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //2.根据课程ID 回显章节对应的课程信息
    public void findCourseById(HttpServletRequest request, HttpServletResponse response){

        try {
            //1.获取参数
            String course_id = request.getParameter("course_id");

            //2.业务处理
            CourseContentService contentService = new CourseContentServiceImpI();
            Course course = contentService.findCourseByCourseId(Integer.parseInt(course_id));

            //3.返回JSON数据
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");

            String jsonResult = JSON.toJSONString(course, filter);

            response.getWriter().print(jsonResult);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 3.保存&修改 章节信息
     */
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response){

        try {
            //1.获取参数，从域对象中获取
            Map<String,Object> map = (Map) request.getAttribute("map");

            //2.创建Course_Section
            Course_Section section = new Course_Section();

            //3.使用BeanUtils工具类，将map中的数据封装到section中
            BeanUtils.populate(section,map);

            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpI();

            //5.判断是否携带id
            if (section.getId() != 0){
                // 修改操作
                String result = contentService.updateSection(section);
                // 响应结果
                response.getWriter().print(result);
            } else {
                // 新增操作
                String result = contentService.saveSection(section);
                //响应结果
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //4.修改章节状态
    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response){

        try {
            //1.接受参数
            int id = Integer.parseInt( request.getParameter("id") );//章节id
            int status = Integer.parseInt( request.getParameter("status") );//章节状态

            //2.业务处理
            CourseContentService contentService = new CourseContentServiceImpI();
            String result = contentService.updateSectionStatus(id, status);

            //3.返回结果
            response.getWriter().print(result);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 5.保存&修改 课时信息
     */
    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response){

        try {
            //1.获取参数，从域对象中获取
            Map<String,Object> map = (Map) request.getAttribute("map");

            //2.创建Course_Lesson对象
            Course_Lesson lesson = new Course_Lesson();

            //3.使用BeanUtils工具类，将map集合中的数据封装到lesson中
            BeanUtils.populate(lesson,map);

            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpI();

            //6.判断是否携带id
            if (lesson.getId() != 0){
                //7.修改操作
                String result = contentService.updateLesson(lesson);
                //响应结果
                response.getWriter().print(result);
            } else {
                //5.新增操作
                String result = contentService.saveLesson(lesson);
                //响应结果
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
